package com.webapp.api;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.webapp.helper.HttpUtils;
import com.webapp.model.UserInfo;
import com.webapp.service.UserInfoService;
import com.webapp.view.Resp;

@RestController
public class MecHelpController extends BaseController{

	private static final Logger LOGGER =LoggerFactory.getLogger(MecHelpController.class);
	
	public static final String BASE_URL = "http://ipms.csnits.com/ipms/service/"; //请求前置链接
//	public static final String BASE_URL = "http://180.169.37.244/ipms/service/";
//	public static final String BASE_URL = "http://localhost:8080/ipms/service/";
	public static final String LOGIN_URL = "S_ZJOR_0801";
	private static final String PARKING_URL = "S_ZJOR_0802";
	private static final String YH_URL = "S_ZJOR_0803";
	private static final String ERROR_URL = "S_ZJOR_0804";//改为在场免费二维码链接
	private static final String PAY_URL = "S_ZJOR_0807";
	private static final String LEAVE_URL = "S_ZJOR_0808";
	private static final String free_URL = "S_ZJOR_0809";

	@Autowired
	private UserInfoService userInfoService;
	
	@RequestMapping(path = "/data/help/getNoPlateCph")
	@ResponseBody
	public Resp noPlate(String openid,String streetNo,String gateNo ) {
		Resp resp = new Resp(false);
		try {
			String carLicense = getCph();
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("carLicense",carLicense);
			data.put("streetNo",streetNo);
			data.put("status","0");
			data.put("gateNo",gateNo);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+"S_ZJPM_13", JSONObject.toJSONString(map));
			JSONObject obj = JSONObject.parseObject(str);
			if(obj!=null&&obj.getInteger("status")!=null&&obj.getInteger("status")==0) {
				UserInfo user = new UserInfo();
				user.setCph(carLicense);
				user.setCreateTime(new Date());
				user.setOpenid(openid);
				user.setType(3);
				user.setNoPlateType("1");
				user.setStreetNo(streetNo);
				userInfoService.save(user);
				return new Resp(user);
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	@RequestMapping(path = "/data/help/noPlateCphOut")
	@ResponseBody
	public Resp noPlateOut(String openid,String streetNo,String gateNo,String carLicense ) {
		Resp resp = new Resp(false);
		try {
			UserInfo user = userInfoService.findBySAndOAndC(openid, carLicense,streetNo,3);
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("carLicense",carLicense);
			data.put("streetNo",streetNo);
			data.put("status","1");
			data.put("gateNo",gateNo);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+"S_ZJPM_13", JSONObject.toJSONString(map));
			JSONObject obj = JSONObject.parseObject(str);
			if(obj!=null&&obj.getInteger("status")!=null&&obj.getInteger("status")==0) {
				if(user!=null) {
					user.setNoPlateType("");
					userInfoService.update(user);
				}
				return new Resp(user);
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	private static String getCph() {
		String str2 = "ABCDEFGHJKLPQS";
		String str = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 5; i++) {
			int number = random.nextInt(34);
			sb.append(str.charAt(number));
		}
		return "临" + str2.charAt(random.nextInt(14)) + sb.toString();
	}

	@RequestMapping(path = "/data/help/login")
	@ResponseBody
	public Resp getNotic(String loginName,String passWord) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("loginName",loginName);
			data.put("passWord",passWord);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+LOGIN_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	@RequestMapping(path = "/data/help/setSession")
	@ResponseBody
	public Resp setSession(String streetNo,HttpServletRequest req) {
		Resp resp = new Resp(false);
		try {
			req.getSession().setAttribute("helpStreet", streetNo);
			return new Resp(true);
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	@RequestMapping(path = "/data/help/parking")
	@ResponseBody
	public Resp parking(String carLicense,String streetNo) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("carLicense",carLicense);
			data.put("streetNo",streetNo);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+PARKING_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	@RequestMapping(path = "/data/help/yh")
	@ResponseBody
	public Resp yh(String orderNo,String streetNo,String freeMoney,String state) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("orderNo",orderNo);
			data.put("streetNo",streetNo);
			data.put("freeMoney",freeMoney);
			data.put("state",state);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+YH_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}

	/**
	 * 获取当前时间戳
	 */
	private static String getCurrentTime() {
		return System.currentTimeMillis() + "";
	}
	
	@RequestMapping(path = "/data/help/error")
	@ResponseBody
	public Resp error(String streetNo,String loginName) {
		String time = getCurrentTime();
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("streetNo",streetNo);
			data.put("loginName",loginName);
			data.put("time",time);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+ERROR_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	@RequestMapping(path = "/data/help/needPay")
	@ResponseBody
	public Resp needPay(String carLicense,String streetNo) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("streetNo",streetNo);
			data.put("carLicense",carLicense);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+PAY_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}
	
	@RequestMapping(path = "/data/help/gateNo")
	@ResponseBody
	public Resp leave(String streetNo,String loginName) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("streetNo",streetNo);
			data.put("loginName",loginName);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+PAY_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}

	@RequestMapping(path = "/data/help/takePole")
	@ResponseBody
	public Resp takePole(String gateNo,String streetNo) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("streetNo",streetNo);
			data.put("gateNo",gateNo);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+LEAVE_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}

	@RequestMapping(path = "/data/help/free")
	@ResponseBody
	public Resp free(String carLicense,String streetNo) {
		Resp resp = new Resp(false);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("streetNo",streetNo);
			data.put("carLicense",carLicense);
			map.put("attr", data);
			String str = HttpUtils.postJson(BASE_URL+free_URL, JSONObject.toJSONString(map));
			return new Resp(JSONObject.parseObject(str));
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		return resp;
	}

}
